package com.jilujia.domain.question;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.question.repository.QuestionTagRepository;
import com.jilujia.framework.domain.AbstractPoEntityManager;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class QuestionTagManager extends AbstractPoEntityManager<Long, QuestionTag, QuestionTagRepository> {

	/**
	 * 保存tag，并且使count数加1
	 * 
	 * @param tagName
	 */
	@Transactional
	public void saveTag(String tagName) {
		QuestionTag existedQuestionTag = this.getRepository().findQuestionTagByName(tagName);
		if (existedQuestionTag == null) {
			QuestionTag questionTag = new QuestionTag();
			questionTag.setName(tagName);
			this.setInstance(questionTag);
		} else {
			this.setInstance(existedQuestionTag);
		}

		QuestionTag questionTag = this.getInstance();
		questionTag.setQuestionCount(questionTag.getQuestionCount() + 1);

		this.save();
	}
}
